package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.pojo.AttendanceSheet;
import com.hqyj.dao.AttendanceSheetMapper;
import com.hqyj.service.IAttendanceSheetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-10-20
 */
@Service
public class AttendanceSheetServiceImpl extends ServiceImpl<AttendanceSheetMapper, AttendanceSheet> implements IAttendanceSheetService {

    @Resource
    AttendanceSheetMapper attendanceSheetMapper;

    @Override
    public HashMap<String,Object> queryAttendancList(AttendanceSheet attendanceSheet) {
        System.out.println("===========+"+attendanceSheet);

        HashMap<String,Object> map = new HashMap<>();
        //定义分页
        //定义分页对象
        //Page<AttendanceSheet> page = new Page<>(attendanceSheet.getPage(),attendanceSheet.getRow());
        //查询分页
        QueryWrapper<AttendanceSheet> queryWrapper = new QueryWrapper<>();

        List<AttendanceSheet> pageList = new ArrayList<>();
        //判断是否进行编号查询
        if (attendanceSheet.getAttendanceId()!= null && attendanceSheet.getAttendanceId() != 0){
            pageList = attendanceSheetMapper.queryByOneId(attendanceSheet.getAttendanceId());
        }else {
            int pagefirst = (attendanceSheet.getPage()-1) * attendanceSheet.getRow();
            int pageSize = attendanceSheet.getRow();

            pageList = attendanceSheetMapper.queryAttendancList(pagefirst,pageSize);


            //进行了关于奖罚金额的排序
            if (attendanceSheet.getJiangfa() == 1){
                pageList = attendanceSheetMapper.queryAttendancListByAsc(pagefirst,pageSize,"reward_punishmen_money");
            }
            if (attendanceSheet.getJiangfa() == 2){
                pageList = attendanceSheetMapper.queryAttendancListByDesc(pagefirst,pageSize,"reward_punishmen_money");

            }
        }
        AttendanceSheet one=null;
        if (!"".equals(attendanceSheet.getType())){
            one = attendanceSheetMapper.queryOne(attendanceSheet);
            System.out.println("查询结果"+one);
            if(one != null){
                map.put("EmployeeId",one.getEmployeeId());
                map.put("Type",one.getType());
                map.put("Num",one.getNum());
            }
        }




        //Page<AttendanceSheet> pageList = attendanceSheetMapper.selectPage(page,queryWrapper);

        //List<AttendanceSheet> pageList = new ArrayList<>();
        //查询有多少条数据
        //分页查询 PageNo页：（PageNo-1）*PageSize，PageSize;








        int total = attendanceSheetMapper.querytotal();
        double d = attendanceSheet.getRow();
        int totalPage = (int)Math.ceil(total/d);

        //查询结果集
        map.put("list",pageList);

        map.put("total",total);
        map.put("totalPage",totalPage);
        map.put("curPage",attendanceSheet.getPage());

        //下一页
        if (attendanceSheet.getPage() == totalPage){
            map.put("nextPage",totalPage);

        }else {
            map.put("nextPage",attendanceSheet.getPage()+1);
        }
        //上一页
        if (attendanceSheet.getPage() == 1){
            map.put("prePage",1);
        }else {
            map.put("prePage",attendanceSheet.getPage()-1);
        }
        return map;

    }



    @Override
    public HashMap<String, Object> insertAttendance(AttendanceSheet attendanceSheet) {

        HashMap<String,Object> map = new HashMap<>();
        int insert = attendanceSheetMapper.insertAttendance(attendanceSheet);
        if (insert == 1){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> updateAttendance(AttendanceSheet attendanceSheet) {
        HashMap<String,Object> map = new HashMap<>();
        int i = attendanceSheetMapper.updateAttendance(attendanceSheet);
        System.out.println("............."+attendanceSheet);
        if (i == 1){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> delAttendance(AttendanceSheet attendanceSheet) {
        System.out.println("............."+attendanceSheet);
        HashMap<String,Object> map = new HashMap<>();
        int i = attendanceSheetMapper.delAttendance(attendanceSheet);

        if (i == 1){
            map.put("info","删除成功");
        }else {
            map.put("info","删除失败");
        }
        return map;
    }
}
